#include "inboxListWidget.h"

inboxListWidget::inboxListWidget(QWidget *parent, contactData *data, inboxListData *_inboxListData, inboxListData *_trushListData):QListWidget(parent)
{
	mInboxListData = _inboxListData;
	mTrushListData = _trushListData;
	mContactData = data;
	mInboxListData->loadinboxXml();
	updateList();
//	smsData tmp;
/*	tmp.type=QString("to");
	tmp.content=cntr("yes i know");
	inboxData tmpInbox;
	tmpInbox.phoneNum=QString("15950513456");
	tmpInbox.mMessageList.push_back(tmp);
	tmpInbox.mMessageList.push_back(tmp);

	mInboxList.push_back(tmpInbox);
	*/
	connect(this,SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(slotItemClicked(QListWidgetItem *)));
}

inboxListWidget::~inboxListWidget()
{
	mInboxListData->saveinboxXml();
}


void inboxListWidget::updateList()
{
	this->clear();
	//将mInboxList中的项目添加到列表中
	for(int i=0; i<mInboxListData->mInboxList.count(); i++)
	{
		QString phoneNum = mInboxListData->mInboxList[i].phoneNum;
		QString name = mContactData->getNameForNum(phoneNum);
		if(name != phoneNum) name+=QString("\t");
		name += QString("\t(%1)").arg(mInboxListData->mInboxList[i].mMessageList.count());
		QListWidgetItem *item = new QListWidgetItem(name);
		this->addItem(item);
	}
}


void inboxListWidget::addinbox()
{
#if 0
	addinboxDialog *dalg = new addinboxDialog();
	QDialog *errordalg = new QDialog();
	dalg->exec();
	if(dalg->result() == 0) 
	{
		delete errordalg;
		delete dalg;
		return;
	}

	inboxData tmpData;
	tmpData.name = dalg->editName->text();
	tmpData.phoneNum = dalg->editPhone->text();
	Qt::CheckState sta = dalg->blackCheckBox->checkState();
	if(sta == Qt::Unchecked)
		tmpData.isBlack = false;
	else
		tmpData.isBlack = true;

		for(int i=0; i<mInboxList.count(); i++)
		{
			if(mInboxList[i].name == tmpData.name)
			{
				errordalg->setWindowTitle(cntr("添加失败"));
				QVBoxLayout *mainVBoxLayout = new QVBoxLayout(errordalg);
				QLabel *errorLabel = new QLabel(cntr("联系人已经存在"), errordalg);
				mainVBoxLayout->addWidget(errorLabel);
				errordalg->exec();
				delete errordalg;
				delete dalg;
				return;
			}
		}
		for(int i=0; i<mInboxListBlack.count(); i++)
		{
			if(mInboxListBlack[i].name == tmpData.name)
			{
				errordalg->setWindowTitle(cntr("添加失败"));
				QVBoxLayout *mainVBoxLayout = new QVBoxLayout(errordalg);
				QLabel *errorLabel = new QLabel(cntr("联系人已经存在"), errordalg);
				mainVBoxLayout->addWidget(errorLabel);
				errordalg->exec();
				delete errordalg;
				delete dalg;
				return;
			}
		}
	if(tmpData.isAvailable())
	{
		if(tmpData.isBlack)
			mInboxListBlack.push_back(tmpData);
		else
			mInboxList.push_back(tmpData);
	}
	updateList();
	delete errordalg;
	delete dalg;
#endif 
}

void inboxListWidget::slotItemClicked(QListWidgetItem *item)
{
	int index = this->currentRow();
	MessageDialog *dalg = new MessageDialog(mInboxListData, mTrushListData, mContactData, index);
	dalg->exec();
	delete dalg;
}


